#include "httpServer.h"

#include "base.qpb.h"
#include "user.qpb.h"
#include "gateway.qpb.h"
#include "friend.qpb.h"
#include "file.qpb.h"
#include "message.qpb.h"
#include "transmite.qpb.h"
#include "speech.qpb.h"
#include "notify.qpb.h"

#include <QDateTime>
#include <QDebug>

HttpServer* HttpServer::instance = nullptr;

HttpServer* HttpServer::getInstance()
{
    static HttpServer instance;
    return &instance;
}

HttpServer::HttpServer()
{

}

//通过这个函数，来配置路由，绑定端口等 进行初始化
bool HttpServer::init()
{
    //返回值是int 成功就是返回端口值
    int ret = httpServer.listen(QHostAddress::Any,8000);
    //配置路由
    httpServer.route("/ping",[](const QHttpServerRequest& req) {
        (void) req;
        qDebug() << "[http] 收到了 ping 请求";
        return "pong";
    });

    httpServer.route("/service/user/get_user_info",[=](const QHttpServerRequest& req){
        return this->getUserInfo(req);
    });

    httpServer.route("/service/friend/get_friend_list",[=](const QHttpServerRequest& req){
        return this->getFriendList(req);
    });

    httpServer.route("/service/friend/get_chat_session_list", [=](const QHttpServerRequest& req) {
        return this->getChatSessionList(req);
    });

    httpServer.route("/service/friend/get_pending_friend_events", [=](const QHttpServerRequest& req) {
        return this->getApplyList(req);
    });

    httpServer.route("/service/message_storage/get_recent", [=](const QHttpServerRequest& req) {
        return this->getRecent(req);
    });

    httpServer.route("/service/message_transmit/new_message", [=](const QHttpServerRequest& req) {
        return this->newMessage(req);
    });

    httpServer.route("/service/user/set_nickname", [=](const QHttpServerRequest& req) {
        return this->setNickname(req);
    });

    httpServer.route("/service/user/set_description", [=](const QHttpServerRequest& req) {
        return this->setDesc(req);
    });

    httpServer.route("/service/user/get_phone_verify_code", [=](const QHttpServerRequest& req) {
        return this->getPhoneVerifyCode(req);
    });

    httpServer.route("/service/user/set_phone", [=](const QHttpServerRequest& req) {
        return this->setPhone(req);
    });

    httpServer.route("/service/user/set_avatar", [=](const QHttpServerRequest& req) {
        return this->setAvatar(req);
    });

    httpServer.route("/service/friend/remove_friend", [=](const QHttpServerRequest& req) {
        return this->removeFriend(req);
    });

    httpServer.route("/service/friend/add_friend_apply", [=](const QHttpServerRequest& req) {
        return this->addFriendApply(req);
    });

    httpServer.route("/service/friend/add_friend_process", [=](const QHttpServerRequest& req) {
        return this->addFriendProcess(req);
    });

    httpServer.route("/service/friend/create_chat_session", [=](const QHttpServerRequest& req) {
        return this->createChatSession(req);
    });

    httpServer.route("/service/friend/get_chat_session_member", [=](const QHttpServerRequest& req) {
        return this->getChatSessionMember(req);
    });

    httpServer.route("/service/friend/search_friend", [=](const QHttpServerRequest& req) {
        return this->searchFriend(req);
    });

    httpServer.route("/service/message_storage/search_history", [=](const QHttpServerRequest& req) {
        return this->searchHistory(req);
    });

    httpServer.route("/service/message_storage/get_history", [=](const QHttpServerRequest& req) {
        return this->getHistory(req);
    });

    httpServer.route("/service/user/username_login", [=](const QHttpServerRequest& req) {
        return this->usernameLogin(req);
    });

    httpServer.route("/service/user/username_register", [=](const QHttpServerRequest& req) {
        return this->usernameRegister(req);
    });

    httpServer.route("/service/user/phone_login", [=](const QHttpServerRequest& req) {
        return this->phoneLogin(req);
    });

    httpServer.route("/service/user/phone_register", [=](const QHttpServerRequest& req) {
        return this->phoneRegister(req);
    });

    httpServer.route("/service/file/get_single_file", [=](const QHttpServerRequest& req) {
        return this->getSingleFile(req);
    });

    httpServer.route("/service/speech/recognition", [=](const QHttpServerRequest& req) {
        return this->recognition(req);
    });

    return ret == 8000;
}

QHttpServerResponse HttpServer::getUserInfo(const QHttpServerRequest &req)
{
    lch_im::GetUserInfoReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 获取用户信息] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    // 构造响应数据
    lch_im::GetUserInfoRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray avatar = loadFileToQByteArray(":/resource/image/groupAvatar.png");
    lch_im::UserInfo userInfo = createUserInfo(1,avatar);
    // userInfo.setUserId("1029");    // 调整自己的用户 id, 和返回的消息列表的内容匹配上
    // userInfo.setNickname("张三");
    // userInfo.setDescription("这是个性签名");
    // userInfo.setPhone("15310432984");
    // userInfo.setAvatar(loadFileToQByteArray(":/resource/image/groupAvatar.png"));
    pbResp.setUserInfo(userInfo);

    QByteArray body = pbResp.serialize(&serializer);

    // 构造响应
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","applicatioon/x-protobuf");
    return httpResp;
}

QHttpServerResponse HttpServer::getFriendList(const QHttpServerRequest &req)
{
    lch_im::GetFriendListReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 获取好友列表] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    // 构造响应数据
    lch_im::GetFriendListRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray avatar = loadFileToQByteArray(":/resource/image/avatarDefault.png");

    for(int i =0; i<30; i++) {
        lch_im::UserInfo userInfo = createUserInfo(i,avatar);
        pbResp.friendList().push_back(userInfo);
    }

    QByteArray body = pbResp.serialize(&serializer);

    // 构造响应
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","applicatioon/x-protobuf");

    return httpResp;
}

QHttpServerResponse HttpServer::getChatSessionList(const QHttpServerRequest &req)
{
    lch_im::GetChatSessionListReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 获取会话列表] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    // 构造响应数据
    lch_im::GetChatSessionListRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray avatar = loadFileToQByteArray(":/resource/image/avatarDefault.png");

    // 构造若干个单聊会话
    for (int i = 0; i < 30; ++i) {
        lch_im::ChatSessionInfo chatSessionInfo;
        chatSessionInfo.setChatSessionId(QString::number(2000 + i));
        chatSessionInfo.setChatSessionName("会话" + QString::number(i));
        chatSessionInfo.setSingleChatFriendId(QString::number(1000 + i));
        chatSessionInfo.setAvatar(avatar);
        lch_im::MessageInfo messageInfo = createTextMessageInfo(i, chatSessionInfo.chatSessionId(), avatar);

        chatSessionInfo.setPrevMessage(messageInfo);

        pbResp.chatSessionInfoList().push_back(chatSessionInfo);
    }

    // 构造一个群聊会话
    QByteArray groupAvatar = loadFileToQByteArray(":/resource/image/groupAvatar.png");
    lch_im::ChatSessionInfo chatSessionInfo;
    chatSessionInfo.setChatSessionId(QString::number(2100));
    chatSessionInfo.setChatSessionName("会话" + QString::number(2100));
    chatSessionInfo.setSingleChatFriendId("");
    chatSessionInfo.setAvatar(groupAvatar);
    lch_im::MessageInfo messageInfo = createTextMessageInfo(0, chatSessionInfo.chatSessionId(), avatar);
    chatSessionInfo.setPrevMessage(messageInfo);
    pbResp.chatSessionInfoList().push_back(chatSessionInfo);


    QByteArray body = pbResp.serialize(&serializer);

    // 构造响应
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","applicatioon/x-protobuf");

    return httpResp;
}

QHttpServerResponse HttpServer::getApplyList(const QHttpServerRequest &req)
{
    lch_im::GetPendingFriendEventListReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 获取会话列表] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    // 构造响应数据
    lch_im::GetPendingFriendEventListRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray avatar = loadFileToQByteArray(":/resource/image/avatarDefault.png");
    for(int i =0; i<5; i++) {
        lch_im::FriendEvent friendEvent;
        friendEvent.setEventId("");         // 这里不需要使用event
        friendEvent.setSender(createUserInfo(i,avatar));
        pbResp.event().push_back(friendEvent);
    }


    QByteArray body = pbResp.serialize(&serializer);

    // 构造响应
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","applicatioon/x-protobuf");

    return httpResp;
}

QHttpServerResponse HttpServer::getRecent(const QHttpServerRequest &req)
{
    lch_im::GetRecentMsgReq  pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 获取最近消息列表] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId();

    // 构造响应数据
    lch_im::GetRecentMsgRsp  pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray avatar = loadFileToQByteArray(":/resource/image/avatarDefault.png");
    for (int i = 0; i < 30; ++i) {
        lch_im::MessageInfo messageInfo = createTextMessageInfo(i, "2000", avatar);
        pbResp.msgList().push_back(messageInfo);
    }
    lch_im::MessageInfo imageMessageInfo = createTextMessageInfo(30, "2000", avatar);
    pbResp.msgList().push_back(imageMessageInfo);
    lch_im::MessageInfo fileMessageInfo = createTextMessageInfo(31, "2000", avatar);
    pbResp.msgList().push_back(fileMessageInfo);
    lch_im::MessageInfo speechMessageInfo = createTextMessageInfo(32, "2000", avatar);
    pbResp.msgList().push_back(speechMessageInfo);


    QByteArray body = pbResp.serialize(&serializer);

    // 构造响应
    QHttpServerResponse httpResp(body,QHttpServerResponse::StatusCode::Ok);
    httpResp.setHeader("Content-Type","applicatioon/x-protobuf");

    return httpResp;
}

QHttpServerResponse HttpServer::newMessage(const QHttpServerRequest &req)
{
    lch_im::NewMessageReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 发送消息] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId() << ", messageType=" << pbReq.message().messageType();

    if (pbReq.message().messageType() == lch_im::MessageTypeGadget::MessageType::STRING) {
        LOG() << "发送的消息内容=" << pbReq.message().stringMessage().content();
    }

    // 构造响应数据
    lch_im::NewMessageRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");

    return resp;
}

QHttpServerResponse HttpServer::setNickname(const QHttpServerRequest &req)
{
    lch_im::SetUserDescriptionReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 修改用户签名] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", desc=" << pbReq.description();

    // 构造响应数据
    lch_im::SetUserDescriptionRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");

    return resp;
}

QHttpServerResponse HttpServer::setDesc(const QHttpServerRequest &req)
{
    lch_im::SetUserNicknameReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 修改用户昵称] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", nickname=" << pbReq.nickname();

    // 构造响应数据
    lch_im::SetUserNicknameRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");

    return resp;
}

QHttpServerResponse HttpServer::getPhoneVerifyCode(const QHttpServerRequest &req)
{
    lch_im::PhoneVerifyCodeReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 获取短信验证码] requestId=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber();

    // 构造响应数据
    lch_im::PhoneVerifyCodeRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);
    pbResp.setVerifyCodeId("testVerifyCodeId");

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");

    return resp;
}

QHttpServerResponse HttpServer::setPhone(const QHttpServerRequest &req)
{
    lch_im::SetUserPhoneNumberReq pbReq;
    pbReq.deserialize(&serializer,req.body());
    LOG() << "[REQ 修改手机号] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId() << ", phone=" << pbReq.phoneNumber()
          << ", verifyCodeId=" << pbReq.phoneVerifyCodeId() << ", verifyCode=" << pbReq.phoneVerifyCode();

    // 构造响应数据
    lch_im::SetUserPhoneNumberRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setErrmsg("");
    pbResp.setSuccess(true);

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");

    return resp;
}

QHttpServerResponse HttpServer::setAvatar(const QHttpServerRequest &req)
{
    lch_im::SetUserAvatarReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 修改头像] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();

    // 构造响应 body
    lch_im::SetUserAvatarRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::removeFriend(const QHttpServerRequest &req)
{
    lch_im::FriendRemoveReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 删除好友] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", peerId=" << pbReq.peerId();

    // 构造响应 body
    lch_im::FriendRemoveRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::addFriendApply(const QHttpServerRequest &req)
{
    lch_im::FriendAddReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 添加好友申请] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", userId=" << pbReq.respondentId();

    // 构造响应 body
    lch_im::FriendAddRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setNotifyEventId("");
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::addFriendProcess(const QHttpServerRequest &req)
{
    lch_im::FriendAddProcessReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 添加好友申请处理] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", applyUserId=" << pbReq.applyUserId() << ", agree=" << pbReq.agree();

    // 构造响应 body
    lch_im::FriendAddProcessRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setNewSessionId("");
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::createChatSession(const QHttpServerRequest &req)
{
    lch_im::ChatSessionCreateReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 创建会话] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", userIdList=" << pbReq.memberIdList();

    // 构造响应 body
    lch_im::ChatSessionCreateRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::getChatSessionMember(const QHttpServerRequest &req)
{
    lch_im::GetChatSessionMemberReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 获取会话成员列表] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId();

    // 构造响应 body
    lch_im::GetChatSessionMemberRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");


    // 循环的构造多个 userInfo, 添加到 memberInfoList 中
    QByteArray avatar = loadFileToQByteArray(":/resource/image/defaultAvatar.png");
    for (int i = 0; i < 10; ++i) {
        lch_im::UserInfo userInfo = createUserInfo(i, avatar);
        pbResp.memberInfoList().push_back(userInfo);
    }
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::searchFriend(const QHttpServerRequest &req)
{
    lch_im::FriendSearchReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 搜索好友] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", searchKey=" << pbReq.searchKey();

    // 构造响应 body
    lch_im::FriendSearchRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");


    QByteArray avatar = loadFileToQByteArray(":/resource/image/avatarDefault.png");
    for (int i = 0; i < 30; ++i) {
        lch_im::UserInfo userInfo = createUserInfo(i, avatar);
        pbResp.userInfo().push_back(userInfo);
    }
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::searchHistory(const QHttpServerRequest &req)
{
    lch_im::MsgSearchReq  pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 搜索历史消息] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId() << ", searchKey=" << pbReq.searchKey();

    // 构造响应 body
    lch_im::MsgSearchRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");


    QByteArray avatar = loadFileToQByteArray(":/resource/image/avatarDefault.png");
    for (int i = 0; i < 10; ++i) {
        lch_im::MessageInfo message = createTextMessageInfo(i, pbReq.chatSessionId(), avatar);
        pbResp.msgList().push_back(message);
    }

    // 构造图片消息
    lch_im::MessageInfo message = createImageMessageInfo(10, pbReq.chatSessionId(), avatar);
    pbResp.msgList().push_back(message);
    // 构造文件消息
    message = createFileMessageInfo(11, pbReq.chatSessionId(), avatar);
    pbResp.msgList().push_back(message);
    // 构造语音消息
    message = createSpeechMessageInfo(12, pbReq.chatSessionId(), avatar);
    pbResp.msgList().push_back(message);

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::getHistory(const QHttpServerRequest &req)
{
    lch_im::GetHistoryMsgReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 按时间搜索历史消息] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId()
          << ", chatSessionId=" << pbReq.chatSessionId() << ", begTime=" << pbReq.startTime() << ", endTime=" << pbReq.overTime();

    // 构造响应 body
    lch_im::GetHistoryMsgRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray avatar = loadFileToQByteArray(":/resource/image/avatarDefault.png");
    for (int i = 0; i < 10; ++i) {
        lch_im::MessageInfo message = createTextMessageInfo(i, pbReq.chatSessionId(), avatar);
        pbResp.msgList().push_back(message);
    }
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::usernameLogin(const QHttpServerRequest &req)
{
    lch_im::UserLoginReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 用户名密码登录] requestId=" << pbReq.requestId() << ", username=" << pbReq.nickname()
          << ", password=" << pbReq.password();

    // 构造响应 body
    lch_im::UserLoginRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setLoginSessionId("testLoginSessionId");

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::usernameRegister(const QHttpServerRequest &req)
{
    lch_im::UserRegisterReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 用户名密码注册] requestId=" << pbReq.requestId() << ", username=" << pbReq.nickname()
          << ", password=" << pbReq.password();

    // 构造响应 body
    lch_im::UserRegisterRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::phoneLogin(const QHttpServerRequest &req)
{
    lch_im::PhoneLoginReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 手机号登录] requestId=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber()
          << ", verifyCodeId=" << pbReq.verifyCodeId() << ", verifyCode=" << pbReq.verifyCode();


    // 构造响应 body
    lch_im::PhoneLoginRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setLoginSessionId("testLoginSessionId");
    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::phoneRegister(const QHttpServerRequest &req)
{
    lch_im::PhoneRegisterReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 手机号注册] requestId=" << pbReq.requestId() << ", phone=" << pbReq.phoneNumber()
          << ", verifyCodeId=" << pbReq.verifyCodeId() << ", verifyCode=" << pbReq.verifyCode();

    // 构造响应 body
    lch_im::PhoneRegisterRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::getSingleFile(const QHttpServerRequest &req)
{
    lch_im::GetSingleFileReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 获取单个文件] requestId=" << pbReq.requestId() << ", fileId=" << pbReq.fileId();


    // 构造响应 body
    lch_im::GetSingleFileRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");

    lch_im::FileDownloadData fileDownloadData;
    fileDownloadData.setFileId(pbReq.fileId());
    if (pbReq.fileId() == "testImage") {
        // fileDownloadData.setFileContent(loadFileToQByteArray(":/resource/image/logo.png"));
        fileDownloadData.setFileContent(loadFileToQByteArray(":/resource/image/avatarDefault.png"));
    } else if (pbReq.fileId() == "testFile") {
        fileDownloadData.setFileContent(loadFileToQByteArray(":/resource/file/test.txt"));
    } else if (pbReq.fileId() == "testSpeech") {
        fileDownloadData.setFileContent(loadFileToQByteArray(":/resource/file/speech.pcm"));
    } else {
        pbResp.setSuccess(false);
        pbResp.setErrmsg("fileId 不是预期的测试 fileId");
    }
    pbResp.setFileData(fileDownloadData);

    QByteArray body = pbResp.serialize(&serializer);

    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}

QHttpServerResponse HttpServer::recognition(const QHttpServerRequest &req)
{
    lch_im::SpeechRecognitionReq pbReq;
    pbReq.deserialize(&serializer, req.body());
    LOG() << "[REQ 语音转文字] requestId=" << pbReq.requestId() << ", loginSessionId=" << pbReq.sessionId();


    // 构造响应 body
    lch_im::SpeechRecognitionRsp pbResp;
    pbResp.setRequestId(pbReq.requestId());
    pbResp.setSuccess(true);
    pbResp.setErrmsg("");
    pbResp.setRecognitionResult("你好你好, 这是一段语音消息, 你好你好, 这是一段语音消息");
    QByteArray body = pbResp.serialize(&serializer);


    // 构造 HTTP 响应
    QHttpServerResponse resp(body, QHttpServerResponse::StatusCode::Ok);
    resp.setHeader("Content-Type", "application/x-protobuf");
    return resp;
}
